package com.xd.employee.controller;

import com.alibaba.druid.support.json.JSONUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;
import com.xd.common.util.SendApiconfig;
import com.xd.employee.model.Employee;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 网上用户
 * 
 * @author andrew
 * @email 1992lcg@163.com
 * @date 2018-07-30 10:33:49
 */
 
@Controller
@RequestMapping("/employee")
public class EmployeeController {
	@Autowired
	private SendApiconfig sendApiconfig;

	@GetMapping("/index")
	@RequiresPermissions("employee:index")
	String employee(){
	    return "Employee/index";
	}

	/**
	 *查询
	 * @param params
	 * @return
	 */
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("employee:index")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
		List<Employee> employeeList = new ArrayList<>();
		int total=0;
		JSONObject postData= new JSONObject();
		postData.put("code","2001");
		postData.put("params", JSONUtils.toJSONString(params));
		try{
			JSONObject jsonObject1= JSONObject.parseObject(sendApiconfig.getPython(postData));
			if( Boolean.valueOf(jsonObject1.get("status").toString())){
				//返回数据
				String data = jsonObject1.get("data").toString();
				employeeList = JSON.parseObject(JSONObject.parseObject(data).get("items").toString(),new TypeReference<List<Employee>>(){});
				//获取总条数
				total = (int) JSONObject.parseObject(jsonObject1.get("data").toString()).get("total");
			}
		}catch (Exception e){
			e.printStackTrace();
		}
		PageUtils pageUtils = new PageUtils(employeeList, total);
		return pageUtils;
	}

	/**
	 * 新增页面
	 * @return
	 */
	@GetMapping("/add")
	String add(){
	    return "employee/add";
	}

	/**
	 * 编辑页面
	 * @param id
	 * @param model
	 * @return
	 */
	@GetMapping("/edit/{id}")
	String edit(@PathVariable("id") Integer id,Model model){
		JSONObject postData= new JSONObject();
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("id",id);
		postData.put("code","2005");
		postData.put("params", jsonObject);
		Employee employee = new Employee();
		try{
		JSONObject jsonObject1= JSONObject.parseObject(sendApiconfig.getPython(postData));
		if( Boolean.valueOf(jsonObject1.get("status").toString())){
			employee = JSON.parseObject(jsonObject1.get("data").toString(),Employee.class);
		}
		}catch (Exception e){
			e.printStackTrace();
		}
		model.addAttribute("employee", employee);
	    return "employee/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	public R save(Employee employee){
		JSONObject postData= new JSONObject();
		postData.put("code","2002");
		postData.put("params", JSONObject.parseObject(JSON.toJSONString(employee)));
		JSONObject jsonObject= new JSONObject();
		try{
		jsonObject= JSONObject.parseObject(sendApiconfig.getPython(postData));
		}catch (Exception e){
			e.printStackTrace();
		}
		if( Boolean.valueOf(jsonObject.get("status").toString())) {
			return R.ok();
		}
		return R.error(jsonObject.get("msg").toString());

	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	public R update(Employee employee){
		JSONObject postData= new JSONObject();
		postData.put("code","2003");
		postData.put("params", JSONObject.parseObject(JSON.toJSONString(employee)));
		JSONObject jsonObject= new JSONObject();
		try{
			jsonObject= JSONObject.parseObject(sendApiconfig.getPython(postData));
		}catch (Exception e){
			e.printStackTrace();
		}
		if( Boolean.valueOf(jsonObject.get("status").toString())){
			return R.ok();
		}
		return R.error(jsonObject.get("msg").toString());
}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	public R remove(Integer id){
		JSONObject postData= new JSONObject();
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("id",id);
		postData.put("code","2004");
		postData.put("params", jsonObject);
		try{
			jsonObject= JSONObject.parseObject(sendApiconfig.getPython(postData));
		}catch (Exception e){
			e.printStackTrace();
		}
		if( Boolean.valueOf(jsonObject.get("status").toString())){
			return R.ok();
		}
		return R.error(jsonObject.get("msg").toString());
	}

}
